﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LandPortMoblieWeb.Models;
using System.Text;

namespace LandPortMoblieWeb.Service
{
    public class MassageService:PSIBaseService
    {
        /// <summary>
        /// 发布通知
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool ReleaseMassage(Massage model)
        {
            var str = new StringBuilder("insert Massage values (@id,@title,@contens,@File1,@File2,@ExpirationDate,@ReleasePrason,@ReleaseDate)");
            var p = CreateDbParameters();
            p.AddWithValue("id",Guid.NewGuid());
            p.AddWithValue("title",model.Title);
            p.AddWithValue("contens",model.Contents);
            p.AddWithValue("File1",model.File1);
            p.AddWithValue("File2",model.File2);
            p.AddWithValue("ExpirationDate",model.ExpriatinDate);
            p.AddWithValue("ReleasePrason",model.ReleasePrason);
            p.AddWithValue("ReleaseDate",model.ReleasDate);

            var result=Execute(str.ToString(), p);
            if (result > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 编辑通知
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool EditMassage(Massage model)
        {
            var str = new StringBuilder("update Massage set title=@title,Contents=@contens,File1=@File1,File2=@File2,ExpirationDate=@ExpirationDate where id=@id");
            var p = CreateDbParameters();
            p.AddWithValue("id", model.Id);
            p.AddWithValue("title", model.Title);
            p.AddWithValue("contens", model.Contents);
            p.AddWithValue("File1", model.File1);
            p.AddWithValue("File2", model.File2);
            p.AddWithValue("ExpirationDate", model.ExpriatinDate);
      
            var result = Execute(str.ToString(), p);
            if (result > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelMassage(string id)
        {
            var str = new StringBuilder("delete Massage where id=@id");
            var p = CreateDbParameters();
            p.AddWithValue("id",id);
            var result = Execute(str.ToString(), p);
            if (result > 0)
            {
                return true;
            }
            return false;
        
        }
        /// <summary>
        /// 返回通知列表集合
        /// </summary>
        /// <returns></returns>
        public List<Massage> GetList(bool ff)
        {
            var result = new List<Massage>();
            var str= new StringBuilder("select * from  Massage where 1=1 ");
            if (ff)
            {
                str.Append(" and getdate()<ExpirationDate");
            }
            str.Append(" order by ReleaseDate desc");
            Query(str.ToString(), ready =>
            {
                result.Add(
                    new Massage() { 
                      Id=ready.GetGuid(ready.GetOrdinal("Id")).ToString(),
                      Title = ready.GetString(ready.GetOrdinal("Title")),
                      Contents = ready.GetString(ready.GetOrdinal("Contents")),
                      File1 = ready.GetString(ready.GetOrdinal("File1")),
                      File2 = ready.GetString(ready.GetOrdinal("File2")),
                      ExpriatinDate = ready.GetDateTime(ready.GetOrdinal("ExpirationDate")),
                      ReleasDate = ready.GetDateTime(ready.GetOrdinal("ReleaseDate")),
                      ReleasePrason = ready.GetString(ready.GetOrdinal("ReleasePrason"))
                    }
                  );

            });

            return result;
        }

        /// <summary>
        /// 得到指定的通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Massage GetSinlge(string id)
        {
            var result = new Massage();
            var str = new StringBuilder("select * from  Massage where Id=@id ");
            var p = CreateDbParameters();
            p.AddWithValue("id",id);
            
            Query(str.ToString(),p, ready =>
            {

                result = new Massage()
                  {
                      Id = ready.GetGuid(ready.GetOrdinal("Id")).ToString(),
                      Title = ready.GetString(ready.GetOrdinal("Title")),
                      Contents = ready.GetString(ready.GetOrdinal("Contents")),
                      File1 = ready.GetString(ready.GetOrdinal("File1")),
                      File2 = ready.GetString(ready.GetOrdinal("File2")),
                      ExpriatinDate = ready.GetDateTime(ready.GetOrdinal("ExpirationDate")),
                      ReleasDate = ready.GetDateTime(ready.GetOrdinal("ReleaseDate")),
                      ReleasePrason = ready.GetString(ready.GetOrdinal("ReleasePrason"))
                  };
                  

            });

            return result;

        }

    }
}